package com.neu.service.impl;

import com.neu.domain.Activity;
import com.neu.mapper.ActivityMapper;
import com.neu.service.ActivityService;
import com.neu.util.ConstUtil;
import com.neu.vo.ActivityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    ActivityMapper activityMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public String skill(int activityId, int customerId) {
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<Long>(ConstUtil.SKillScript, Long.class);
        List<String> paramList = new ArrayList<>();
        paramList.add(customerId+"");
        paramList.add(activityId+"");
        Long result = (Long) redisTemplate.execute(redisScript, paramList);
        if (result == 0) {
            return "该用户已抢过";
        } else if (result == 1) {
            return "已抢空";
        } else if (result == 2) {
            return "抢购成功";
        }
        else if (result == 3) {
            return "抢购没开始";
        }else {
            return "抢购异常";
        }
    }

    @Override
    public int add(Activity activity) {
        return activityMapper.add(activity);
    }

    @Override
    public List<ActivityVO> find() {
        return activityMapper.find();
    }
}
